import 'package:flutter/material.dart';
import 'package:english_words/english_words.dart';
import '../util/customRouter.dart';

class MyListView extends StatefulWidget {
  const MyListView({Key? key}) : super(key: key);

  @override
  _MyListViewState createState() => _MyListViewState();
}

class _MyListViewState extends State<MyListView> with AutomaticKeepAliveClientMixin {
  @override
  bool get wantKeepAlive => true;

  late List<Map> list;

  @override
  void initState() {
    list = [
      {'title': '好看的小姐姐1号', 'icon': Icons.favorite, 'love': false},
      {'title': '好看的小姐姐2号', 'icon': Icons.favorite, 'love': false},
      {'title': '好看的小姐姐3号', 'icon': Icons.favorite, 'love': false},
      {'title': '好看的小姐姐4号', 'icon': Icons.favorite, 'love': false},
      {'title': '好看的小姐姐5号', 'icon': Icons.favorite, 'love': false},
      {'title': '好看的小姐姐6号', 'icon': Icons.favorite, 'love': false},
      {'title': '好看的小姐姐7号', 'icon': Icons.favorite, 'love': false},
      {'title': '好看的小姐姐8号', 'icon': Icons.favorite, 'love': false},
      {'title': '好看的小姐姐9号', 'icon': Icons.favorite, 'love': false},
      {'title': '好看的小姐姐10号', 'icon': Icons.favorite, 'love': false},
      {'title': '好看的小姐姐11号', 'icon': Icons.favorite, 'love': false},
      {'title': '好看的小姐姐12号', 'icon': Icons.favorite, 'love': false},
      {'title': '好看的小姐姐13号', 'icon': Icons.favorite, 'love': false},
      {'title': '好看的小姐姐14号', 'icon': Icons.favorite, 'love': false},
      {'title': '好看的小姐姐15号', 'icon': Icons.favorite, 'love': false},
      {'title': '好看的小姐姐16号', 'icon': Icons.favorite, 'love': false},
      {'title': '好看的小姐姐17号', 'icon': Icons.favorite, 'love': false},
      {'title': '好看的小姐姐18号', 'icon': Icons.favorite, 'love': false},
      {'title': '好看的小姐姐19号', 'icon': Icons.favorite, 'love': false},
      {'title': '好看的小姐姐20号', 'icon': Icons.favorite, 'love': false}
    ];
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return ListView.builder(
      padding: const EdgeInsets.all(16.0),
      itemCount: list.length,
      itemBuilder: (context, index) {
        return Container(
          decoration: const BoxDecoration(
            border: Border(bottom: BorderSide(width: 1.0)),
          ),
          child: ListTile(
            title: Text(list[index]['title']),
            leading: Icon(
              list[index]['icon'],
              color: list[index]['love'] ? Colors.red : Colors.grey,
            ),
            onTap: () async {
              final result = await Navigator.of(context).push(CustomRouter(
                  ListDetail( item: list[index], changeLove: (isLove) =>_changeLove(isLove, list[index]))
              ));
              setState(() {
                if (result == 'delete') {
                  list.removeAt(index);
                }
              });
            },
          ),
        );
      },
    );
  }

  void _changeLove(isLove, item) {
    setState(() {
      item['love'] = isLove;
    });
  }
}

class ListDetail extends StatefulWidget {
  const ListDetail({
    Key? key,
    required this.item,
    required this.changeLove,
  }) : super(key: key);
  final Map item;
  final Function changeLove;

  @override
  _ListDetailState createState() => _ListDetailState();
}

class _ListDetailState extends State<ListDetail> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('小姐姐详情'),
      ),
      body: Padding(
        padding: const EdgeInsets.fromLTRB(20.0, 0, 20.0, 0),
        child: Row(
      children: [
        Expanded(
          child: Text(
            '${widget.item['title']}',
            style: const TextStyle(color: Colors.lime, fontSize: 16.0),
          ),
        ),
        IconButton(
            onPressed: () {
              setState(() {
                widget.changeLove(!widget.item['love']);
              });
            },
            icon: Icon(Icons.favorite,
                color: widget.item['love'] ? Colors.red : Colors.grey)),
        ElevatedButton(
          onPressed: () {
            Navigator.of(context).pop('delete');
          },
          child: const Text(
            '删除',
            style: TextStyle(fontSize: 12.0),
          ),
          style: ButtonStyle(
            backgroundColor: MaterialStateProperty.all(Colors.grey),
            padding: MaterialStateProperty.all(const EdgeInsets.all(0.0)),
            minimumSize: MaterialStateProperty.all(const Size(40.0, 25.0)),
          ),
        )
      ],
        ),
      ),
    );
  }
}
